package com.wanli.agent.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

/**
 * 类描述与备注
 *
 * @author 客尘(衡文闯)
 * @version $Id: PopWinDownUtil, v1.0.0 2018年11月13日 17:07 客尘(衡文闯) Exp $
 */
public class PopWinDownUtil {
    private Activity context;
    private View contentView;
    private View relayView;
    private PopupWindow popupWindow;

    public PopWinDownUtil(Activity context, View contentView, View relayView) {
        this.context = context;
        this.contentView = contentView;
        this.relayView = relayView;
        init();
    }

    @SuppressLint("WrongConstant")
    public void init() {
        //内容，高度，宽度
        popupWindow = new PopupWindow(contentView, (int) (ScreenUtils.getScreenWidth(context) - Dp2Px.dp2Px(156)), WindowManager.LayoutParams.WRAP_CONTENT, false);
        //动画效果
//        popupWindow.setAnimationStyle(R.style.AnimationTopFade);
        //菜单背景色
        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        popupWindow.setClippingEnabled(false);
        popupWindow.setBackgroundDrawable(dw);
        popupWindow.setOutsideTouchable(true);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (onDismissLisener != null) {
                    onDismissLisener.onDismiss();
                }
            }
        });
    }

    public void show() {
        //显示位置
//        popupWindow.showAsDropDown(relayView);
        showAsDropDown(popupWindow, relayView, 0, 0);
    }

    public void show(View relayView) {
        //显示位置
        showAsDropDown(popupWindow, relayView, 0, 0);
    }

    /**
     * @param pw     popupWindow
     * @param anchor v
     * @param xoff   x轴偏移
     * @param yoff   y轴偏移
     */
    public void showAsDropDown(final PopupWindow pw, final View anchor, final int xoff, final int yoff) {
        pw.showAsDropDown(anchor, 0, yoff);
    }

    public void hide() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }

    private OnDismissLisener onDismissLisener;

    public void setOnDismissListener(OnDismissLisener onDismissLisener) {
        this.onDismissLisener = onDismissLisener;
    }

    public interface OnDismissLisener {
        void onDismiss();
    }

    public boolean isShowing() {
        return popupWindow.isShowing();
    }
}
